package org.mozhu.mboot.admin.maintain.notification.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.mozhu.mboot.admin.maintain.notification.entity.Notification;
import org.mozhu.mboot.admin.maintain.notification.entity.NotificationTemplate;
import org.mozhu.mboot.admin.maintain.notification.exception.TemplateNotFoundException;
import org.mozhu.mboot.admin.maintain.push.service.PushApi;
import org.mozhu.mboot.core.data.Searchable;
import org.mozhu.mboot.core.data.filter.Operator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Service
public class NotificationApiImpl implements NotificationApi {

	@Autowired
	private NotificationTemplateService notificationTemplateService;

	@Autowired
	private NotificationService notificationService;

	@Autowired
	private PushApi pushApi;

	/**
	 * 异步发送
	 * 
	 * @param userId
	 *            接收人用户编号
	 * @param templateCode
	 *            模板名称
	 * @param context
	 *            模板需要的数据
	 */
	@Async
	@Override
	public void notify(final Long userId, final String templateCode, final Map<String, Object> context) {
		NotificationTemplate template = notificationTemplateService.findByCode(templateCode);

		if (template == null) {
			throw new TemplateNotFoundException(templateCode);
		}

		Notification data = new Notification();

		data.setUserId(userId);
		data.setModule(template.getModule());
		data.setDate(new Date());

		String content = template.getContent();
		String title = template.getTitle();
		if (context != null) {
			for (String key : context.keySet()) {
				// TODO 如果量大可能有性能问题 需要调优
				title = title.replace("{" + key + "}", String.valueOf(context.get(key)));
				content = content.replace("{" + key + "}", String.valueOf(context.get(key)));
			}
		}

		data.setTitle(title);
		data.setContent(content);

		notificationService.save(data);

		pushApi.pushNewNotification(userId, topFiveNotification(userId));

	}

	@Override
	public List<Map<String, Object>> topFiveNotification(final Long userId) {
		List<Map<String, Object>> dataList = Lists.newArrayList();
		Searchable searchable = Searchable.newSearchable();
		searchable.addSearchFilter("userId", Operator.EQ, userId);
		// searchable.addSearchFilter("read", SearchOperator.eq, Boolean.FALSE);
		searchable.addSort(Sort.Direction.DESC, "id");
		searchable.setPage(0, 5);
		Page<Notification> page = notificationService.findAll(searchable);
		for (Notification data : page.getContent()) {
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", data.getId());
			map.put("title", data.getTitle());
			map.put("content", data.getContent());
			map.put("read", data.getRead());
			map.put("date", data.getDate());
			dataList.add(map);
		}
		return dataList;
	}
}
